package com.stox.downloader.yahoo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;

import com.stox.core.model.Exchange;
import com.stox.core.model.Scrip;
import com.stox.core.util.Constant;
import com.stox.downloader.DataProvider;
import com.stox.downloader.DataStream;

public class YahooDataProvider implements DataProvider {

	private String downloadSpanUrlYahooStart = "http://ichart.finance.yahoo.com/table.csv?s=";
	private String downloadSpanUrlYahooEnd = "ignore=.csv";
	private String startDateParam = "b";
	private String startMonthParam = "a";
	private String startYearParam = "c";
	private String endDateParam = "e";
	private String endMonthParam = "d";
	private String endYearParam = "f";
	private String timeUnitParam = "g";
	private String dailyTimeUnitValue = "daily";
	
	private YahooDataStreamParser yahooDataStreamParser;
	
	
	@Override
	public String getName() {
		return "Yahoo Finance";
	}

	@Override
	public int getOrder() {
		return 1;
	}
	
	@SuppressWarnings("deprecation")
	public String constructSpanUrl(String id, String exchangeCode, Date start, Date end){
		return downloadSpanUrlYahooStart+
				id+"."+exchangeCode+"&"+
				timeUnitParam+"="+dailyTimeUnitValue+"&" +
				startDateParam+"="+start.getDate()+"&" +
				startMonthParam+"="+start.getMonth()+"&" +
				startYearParam+"="+(start.getYear()+1900)+"&" +
				endDateParam+"="+end.getDate()+"&" +
				endMonthParam+"="+end.getMonth()+"&" +
				endYearParam+"="+(end.getYear()+1900)+"&" +
				downloadSpanUrlYahooEnd;
	}

	@Override
	public List<Scrip> getScrips() {
		throw new UnsupportedOperationException();
	}

	@Override
	public DataStream get(Scrip scrip, Date start, Date end) {
		try {
			URLConnection connection = new URL(constructSpanUrl(scrip.getId(), scrip.getExchangeCode(), start, end)).openConnection();
			connection.setConnectTimeout(30*1000);
			connection.setReadTimeout(30*1000);
			 InputStream inputStream = connection.getInputStream();
			 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			 String line = null;
			 StringBuffer stringBuffer = new StringBuffer();
			 while((line = bufferedReader.readLine()) != null){
				 stringBuffer.append(line+Constant.lineSeparator);
			 }
			 String s = stringBuffer.toString();
			 YahooDataStream dataStream = new YahooDataStream();
			 dataStream.setBarList(yahooDataStreamParser.parse(s, scrip));
			 return dataStream;
		}catch(FileNotFoundException fnfe){
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public DataStream get(Exchange exchange, Date date) {
		throw new UnsupportedOperationException();
	}

	public YahooDataStreamParser getYahooDataStreamParser() {
		return yahooDataStreamParser;
	}

	public void setYahooDataStreamParser(YahooDataStreamParser yahooDataStreamParser) {
		this.yahooDataStreamParser = yahooDataStreamParser;
	}


}
